/*
 * TicketsDaoJdbc.java      1.0   24/02/2011
 *
 * Copyright Grupo 3A UPC
 * All rights reserved.
 *
 * Contiene el manejador JDBC para los Tickets
 */
package pe.edu.upc.dew.grupo3a.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import pe.edu.upc.dew.grupo3a.model.Ticket;

public class TicketsDaoJdbc extends SimpleJdbcDaoSupport implements
TicketsDao{

	ReservaDao reservaDao;
	
	public void setReservaDao(ReservaDao reservaDao) {
		this.reservaDao = reservaDao;
	}

	@Override
	public void insertarTicket(Ticket ticket) {
		getSimpleJdbcTemplate().update("insert into ticket (codigoticket," +
			"codigoreserva,fechaticket) values (?,?,?)",
			ticket.getNumero(),ticket.getReserva().getCodigo(),
			ticket.getFecha());
	}

	@Override
	public Ticket getTicketPorCodigo(String codigo) {
		return getSimpleJdbcTemplate()
		.queryForObject(
				"select codigoticket,codigoreserva,fechaticket "+
				"from ticket where codigoticket=?",
				new RowMapper<Ticket>(){
					public Ticket mapRow(ResultSet rs, int rowNum) throws SQLException {
						Ticket ticket = new Ticket();
						ticket.setFecha(rs.getDate("fechaticket"));
						ticket.setNumero(rs.getString("codigoticket"));
						ticket.setReserva(reservaDao.getReservaPorCodigo(rs.getString("codigoreserva")));
						return ticket;
					}
				},
				codigo);
	}

}
